import React, { Component } from 'react';
import { withRouter } from 'react-router-dom';
const queryString = require('query-string');
import InspectionWeekTable from './components/InspectionWeekTable';
import './index.scss';
import request from "../../instances/request";
import { minHeight } from "../../conf/config";

@withRouter
class InspectionWeekList extends Component {
  constructor(props) {
    super(props);

    this.state = {
      __loading: true,
      title: '',
      type: '',
      isBoweiSupport: false,
      createdTime: '',
    }
  }

  async getAndSetUserPrivilege() {
    let res = await request.get('/personalData');
    let { code, msg } = res.data;
    if( code !== 0 ) {
      throw new Error(`code为${code}`);
    }
    let { user } = msg;
    let isBoweiSupport = user.browsePrivileges.inspectionSupport;
    this.setState({
      isBoweiSupport,
    });
  }

  async componentDidMount() {
    await this.getAndSetUserPrivilege();
    const { location } = this.props;
    let { oid, title, type, createdTime } = queryString.parse(location.search);
    this.setState({
      __loading: false,
      title,
      oid,
      createdTime: createdTime || ''
    });
  }

  render() {
    return (
      this.state.__loading ||
      <div className="inspection-record-day" style={{ minHeight: `${minHeight}px` }}>
        <InspectionWeekTable
          limit={24}
          title={this.state.title}
          oid={this.state.oid}
          type={this.state.type}
          createdTime={this.state.createdTime}
          isBoweiSupport={this.state.isBoweiSupport}
        />
      </div>
    )
  }
}

export default InspectionWeekList;
